<?php

namespace app\models;

use Yii;
use yii\db\Query;

/**
 * This is the model class for table "bf_investor".
 *
 * @property int $id
 * @property string|null $holder 持有人
 * @property int|null $card_num 卡片数量
 * @property float|null $total_credit 累计授信额度
 * @property float|null $total_used 占用额度
 * @property string|null $end_time 合作截止时间
 * @property int|null $status 状态：1正常2暂停3已过期
 * @property string|null $created_time 加入时间
 */
class Investor
{
    //账户列表（资方）
    public function accountlist()
    {

        $listsql="select a.card,a.bank_name,a.credit_limit,a.use_limit,a.end_time,a.status,b.logo From bf_bank a LEFT JOIN bf_bank_info b ON b.bank_name = a.bank_name";
        $accountlist=Yii::$app->db->createCommand($listsql)->queryAll();
        foreach ($accountlist as $k => $y) {
            $accountlist[$k]['balance_credit'] = number_format($y['credit_limit'] - $y['use_limit'], 2);
        }
        return ['code' => 200, 'message' => 'OK', 'data' => $accountlist];
    }

    //账户详情（资方）
    public function detailaccount($get)
    {
        if(!empty($get)){
            $accountsql="select a.card,a.bank_name,a.credit_limit,a.use_limit,a.end_time,a.status,b.logo From bf_bank a LEFT JOIN bf_bank_info b ON b.bank_name = a.bank_name where a.user_id=".$get['user_id'];
            $account=Yii::$app->db->createCommand($accountsql)->queryOne();
            $repaysql="select a.order_number,a.info_name,a.df_time,a.dates,a.df_money,a.interest,a.status From bf_repay a where a.investor_id =".$get['user_id']." AND a.status=".$get['status'];
            $repay=Yii::$app->db->createCommand($repaysql)->queryAll();
            $date=[
                'res'=>$account,
                'list'=>$repay
            ];
            return ['code' => 200, 'message' => 'OK', 'data' => $date];
        }else {
            return ['code' => 401, 'message' => '参数缺失', 'data' => ''];
        }

    }

    //资方列表
    public function userlist()
    {
        $userlist = Yii::$app->db->createCommand('select * from bf_investor')->queryAll();
        foreach ($userlist as $k => $y) {
            $userlist[$k]['balance_credit'] = number_format($y['total_credit'] - $y['total_used'], 2);
        }
        return ['code' => 200, 'message' => 'OK', 'data' => $userlist];
    }

    //资方详情
    public function userdetail($get)
    {
        if (!empty($get)) {
            $detailsql = "select a.holder,a.total_credit,a.total_used,a.end_time,a.status,a.created_time,a.account From bf_investor a where a.id=" . $get['id'];
            $banksql = "select b.card,b.credit_limit,b.binding_time,b.end_time,b.total_money,b.df_date,b.total_income,b.status From bf_bank b where b.user_id=" . $get['user_id'];
            $detail = Yii::$app->db->createCommand($detailsql)->queryOne();
            $detail['balance_credit'] = number_format($detail['total_credit'] - $detail['total_used'], 2);
            $bank = Yii::$app->db->createCommand($banksql)->queryAll();
            $date = [
                'userinfor' => $detail,
                'bankcard' => $bank
            ];
            return ['code' => 200, 'message' => 'OK', 'data' => $date];
        } else {
            return ['code' => 401, 'message' => '参数缺失', 'data' => ''];
        }
    }

    //资方用户冻结
    public function frozen($post)
    {
        if (!empty($post)) {
            $db_update = Yii::$app->db->createCommand('UPDATE bf_customer SET status=' . $post['status'] . ' WHERE id=' . $post['id'])->execute();
            return ['code' => 200, 'message' => 'OK', 'data' => ''];
        } else {
            return ['code' => 401, 'message' => '参数缺失', 'data' => ''];
        }

    }

    //资方银行卡冻结
    public function frozencard($post)
    {
        if (!empty($post)) {
            $db_update = Yii::$app->db->createCommand('UPDATE bf_bank SET status=' . $post['status'] . ' WHERE id=' . $post['id'])->execute();
            return ['code' => 200, 'message' => 'OK', 'data' => ''];
        } else {
            return ['code' => 401, 'message' => '参数缺失', 'data' => ''];
        }

    }
}
